package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPerfessionalTeamPictureBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseModule;
import com.sgb.kjwl.model.entity.enterprise.ProfessionalTeamPictureEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.view.ui.activity.PhotoViewActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.ProfessionalTeamPictureAdapter;
import com.sgb.kjwl.viewmodel.enterprise.ProfessionalTeamPictureModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class ProfessionalTeamPictureActivity extends AppActivity {

    ActivityPerfessionalTeamPictureBinding binding;

    @Inject
    ProfessionalTeamPictureModel professionalTeamPictureModel;

    private boolean isLoadMore = false;
    private int pageNum;
    private int teamNo;
    boolean isUserCenter = false;
    private ProfessionalTeamPictureAdapter professionalTeamPictureAdapter;
    private List<String> list = new ArrayList<>();

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseComponent.builder().appComponent(appComponent)
                .enterpriseModule(new EnterpriseModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected boolean statusBarDark() {
        if (!isUserCenter)
            isUserCenter = getIntent().getBooleanExtra("isUserCenter", false);
        return isUserCenter;
    }

    @Override
    protected void initView() {
        super.initView();
        binding = DataBindingUtil.setContentView(this, R.layout.activity_perfessional_team_picture);

        teamNo = getIntent().getIntExtra("teamNo",0);
        binding.include.titleView.setText("团队相册");
        binding.include.ivBack.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(view -> finish());
        if (isUserCenter) {
            binding.include.liuhaiStatusView.setBackgroundResource(R.color.white);
            binding.include.rlTitle.setBackgroundResource(R.color.white);
            binding.include.ivBack.setImageResource(R.mipmap.btn_back3);
            binding.include.titleView.setTextColor(Color.BLACK);
        } else {
            binding.include.liuhaiStatusView.setBackgroundResource(R.mipmap.navigation_bar);
            binding.include.rlTitle.setBackgroundResource(R.mipmap.navigation_bar);
            binding.include.ivBack.setImageResource(R.mipmap.back);
            binding.include.titleView.setTextColor(Color.WHITE);
        }
        professionalTeamPictureAdapter = new ProfessionalTeamPictureAdapter(this,null);
        binding.displayView.setLayoutManager(new GridLayoutManager(this,2));
        binding.displayView.setAdapter(professionalTeamPictureAdapter);
        setDataLoadMore(true);
        binding.displayView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                setDataLoadMore(true);
            }

            @Override
            public void onLoadMore() {
                setDataLoadMore(false);
            }
        });

        professionalTeamPictureAdapter.setOnItemClick(new AdapterOnItemClick<ProfessionalTeamPictureEntity>() {
            @Override
            public void onItemClick(ProfessionalTeamPictureEntity professionalTeamPictureEntity, int position) {
                list.clear();
                for (ProfessionalTeamPictureEntity data : professionalTeamPictureAdapter.getDatas()) {
                    list.add(data.getUpload_pic());
                }
                ActivityJump.showBigPicture(ProfessionalTeamPictureActivity.this,list,position);
            }
        });
    }

    private void setDataLoadMore(boolean isRefresh) {
        if (isRefresh) {
            isLoadMore = false;
            pageNum = 1;
        } else {
            isLoadMore = true;
            pageNum++;
        }

        if (isUserCenter) {
            professionalTeamPictureModel.getPictureUserCenter(pageNum,20);
        } else {
            professionalTeamPictureModel.getPicture(teamNo+"",pageNum,20);
        }
    }

    public void setShowPicture(List<ProfessionalTeamPictureEntity> list) {
        binding.displayView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.displayView.loadMoreComplete();
                professionalTeamPictureAdapter.addDatas(list);
            } else {
                binding.displayView.setNoMore(true);
            }
            binding.displayView.setVisibility(View.VISIBLE);
        } else {
            binding.displayView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.emptyView.setVisibility(View.GONE);
                professionalTeamPictureAdapter.setDatas(list);
                binding.displayView.setLoadingMoreEnabled(true);
            } else {
                professionalTeamPictureAdapter.clearDatas();
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}